package com.zzld.modules.sys.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zzld.core.utils.MessageUtils;
import com.zzld.core.utils.UuidUtil;
import com.zzld.modules.common.dto.CommonJSON;
import com.zzld.modules.common.dto.TreeGrid;
import com.zzld.modules.sys.entity.Res;
import com.zzld.modules.sys.entity.Role;
import com.zzld.modules.sys.security.ShiroUser;
import com.zzld.modules.sys.service.IResService;
import com.zzld.modules.sys.service.IRoleService;

/**
 * 资源类
 * 
 * @author zzld
 */
@Controller
@RequestMapping(value = "/sys/res")
public class ResController {

	@Autowired
	private IResService ResService;

	@Autowired
	private IRoleService roleService;

	private String listurl = "/modules/sys/res/resList";
	private String editurl = "/modules/sys/res/resCru";

	@RequestMapping(method = RequestMethod.GET)
	public String res(Model model) {
		ShiroUser userq = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		model.addAttribute("userid", userq.getId());
		return listurl;
	}

	/**
	 * 进入修改页面
	 */
	@RequestMapping(value = "/update/pre")
	public String edit(Model model, HttpServletRequest request)
			throws Exception {
		return editurl;
	}

	/**
	 * 根据ID获取数据
	 * 
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/selectById")
	@ResponseBody
	public Res selectById(HttpServletRequest request,
			@RequestParam("id") String id) {
		Res res = null;
		if (id != null && !id.equals("")) {
			try {
				return ResService.selectByPrimaryKey(id);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return res;
	}

	/**
	 * 新增/修改操作
	 * 
	 * @param model
	 * @param entity
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public CommonJSON update(Model model, Res entity, HttpServletRequest request) {
		CommonJSON resultJson = new CommonJSON();
		ShiroUser userq = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		entity.setCreateBy(userq.getName());
		if (StringUtils.isBlank(entity.getId())) {// 新增
			entity.setId(UuidUtil.uuid().toString());// 自己生成id

			try {
				ResService.save(entity);
				resultJson.setSuccess(true);
				resultJson.setMessage(MessageUtils.ADD_SUCCESS);
			} catch (Exception e) {
				e.printStackTrace();
				resultJson.setSuccess(false);
				resultJson.setMessage(MessageUtils.ADD_FAIL);
			}
		} else {
			try {
				ResService.updateByPrimaryKeySelective(entity);
				resultJson.setSuccess(true);
				resultJson.setMessage(MessageUtils.EDIT_SUCCESS);
			} catch (Exception e) {
				e.printStackTrace();
				resultJson.setSuccess(false);
				resultJson.setMessage(MessageUtils.EDIT_WARING);
			}
		}
		return resultJson;
	}

	/**
	 * 删除操作
	 * 
	 * @throws Exception
	 */
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	@ResponseBody
	public CommonJSON delete(HttpServletRequest request,
			@PathVariable("id") String id) {
		CommonJSON resultJson = new CommonJSON();
		int pid = ResService.selectbypid(id);
		// 执行删除
		try {
			if (pid == 0) {
				if (ResService.deleteByPrimaryKey(id) > 0) {
					resultJson.setMessage(MessageUtils.DELETE_SUCCESS);
				} else {
					resultJson.setMessage(MessageUtils.EDIT_WARING);
				}
			} else {
				resultJson.setMessage("请先删除子节点!");
			}

		} catch (Exception e) {
			e.printStackTrace();
			resultJson.setMessage(MessageUtils.EDIT_WARING);
		}
		return resultJson;
	}

	@RequestMapping(value = "treeGrid")
	@ResponseBody
	public List<TreeGrid> treeGrid(HttpServletRequest request, Model model) {
		return ResService.treeGrid();
	}

	@RequestMapping(value = "treegridbyid")
	@ResponseBody
	public List<TreeGrid> treeGrids(HttpServletRequest request, Model model) {
		String id = request.getParameter("resid") == null ? "" : request
				.getParameter("resid");

		if (id != "" && id != null) {
			Role r;
			try {
				r = roleService.selectByPrimaryKey(id);
				return ResService.treeGridbyroleid(r.getId());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// List<TreeGrid> trw = ResService.treeGridbyroleid(r.getId());
			
		}

		// 直接输出资源列表 条件status=1
		return ResService.treeGridbyroleid(request.getParameter("roleid"));
	}

}
